/*
 *	testsuite.m
 *
 *	David Janes
 *	Discover Anywhere Mobile
 *	March 2010
 *
 *	Copyright 2010 Discover Anywhere Mobile
 *
 *	Purpose:
 *	Run the "real" Django tests
 *
 *	Programmers:
 *	Look at the Python files in ../tests for more tests
 *	to run. If you want to tackle something, add them
 *	to 'testsuite.json' and off you go
 */

#import <Foundation/Foundation.h>
#import "DjoltTemplate.h"

main()
{
	id pool = [[NSAutoreleasePool alloc] init];  

	/*
	 *	Tests to run are listed here
	 */
	NSData* tdata = [NSData dataWithContentsOfFile:@"testsuite.json"];
	NSArray* ts = [NSJSONSerialization JSONObjectWithData:tdata options:0 error:nil];

	for (NSString* testname in ts) {
		if ((id) testname == [NSNull null]) {
			continue;
		}

		NSData* rdata = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@.json", testname]];
		NSDictionary* rd = [NSJSONSerialization JSONObjectWithData:rdata options:0 error:nil];

		NSString* expected_result = [rd objectForKey:@"__RESULT"];
		BOOL expected_syntax_error = [[rd objectForKey:@"__SYNTAX_ERROR"] boolValue];

		BOOL got_syntax_error = NO;
		NSString* got_result = nil;

		@try {
			DjoltTemplate* t = [[DjoltTemplate alloc] initWithTemplateFile:[NSString stringWithFormat:@"%@.djolt", testname]];
			got_result = [t render:rd];
		} 
		@catch (NSException *exception) {
			got_syntax_error = YES;
			NSLog(@"EXCEPTION=%@", exception);
		}

		BOOL dump = NO;
		NSString* error = nil;
		if (expected_syntax_error) {
			if (!got_syntax_error) {
				error = @"expected syntax error, didn't get one";
				dump = YES;
			}
		} else if (expected_result) {
			if (got_syntax_error) {
				error = @"expected result, got syntax error";
			} else if (!got_result) {
				error = @"expected result, got nothing";
			} else if ([got_result compare:expected_result]) {
				error = @"expected and actual result differed";
				dump = YES;
			}
		} else {
			error = @"no expected result or syntax error? bad test I'm sure";
		}

		if (!error) {
			NSLog(@"%@: OK", testname);
		} else {
			if (dump) {
				NSLog(@"%@: ERROR: %@\n  EXPECTED: %@\n  GOT: %@", testname, error, expected_result, got_result);
			} else {
				NSLog(@"%@: ERROR: %@", testname, error);
			}
		}
	}

	[pool release];
}
